package com.bwc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.bwc.common.BaseContext;
import com.bwc.common.R;
import com.bwc.domain.ShoppingCart;
import com.bwc.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("add")
    public R<ShoppingCart> save(@RequestBody ShoppingCart shoppingCart){

        //给新来的记录，设置下单人ID
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查找数据库中有没有这个 菜品 或则 套餐的 记录，如果有就数量+1 如果没有 就加进去
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        if(dishId!=null){
            //增加的是菜品
            lambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //增加的是套餐
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
        }
        //查记录有无 sql select * from shopping_cart where dish_id/setmeal_id = ? and user_id = ?
        ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper);

        if(one!=null){
            LambdaUpdateWrapper<ShoppingCart> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.set(ShoppingCart::getNumber,one.getNumber()+1);
            lambdaUpdateWrapper.eq(ShoppingCart::getId,one.getId());
            shoppingCartService.update(lambdaUpdateWrapper);
            one.setNumber(one.getNumber()+1);
        }else {
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
            return R.success(one);
    }

    @PostMapping("sub")
    public R<String> delete(@RequestBody ShoppingCart shoppingCart){
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        if(dishId!=null){
            lambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //sql: select count(*) from 表 where user_id = ? and dish_id/setmeal_id = ?
        ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper);
        if(one.getNumber() > 1){
            //update 表 set number = ? where id = ? and dish_id/setmeal_id = ?
            one.setNumber(one.getNumber()-1);
            shoppingCartService.updateById(one);

        }else {
            shoppingCartService.removeById(one);
            one = null;
        }
        return R.success("成功删除");
    }


    @GetMapping("list")
    public R<List<ShoppingCart>> list(){
        LambdaUpdateWrapper<ShoppingCart> lambdaUpdateWrapper =new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list(lambdaUpdateWrapper);
        return R.success(list);
    }

    @DeleteMapping("clean")
    public R<String> clean(){
        LambdaUpdateWrapper<ShoppingCart> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(lambdaUpdateWrapper);
        return R.success("已清空购物车");
    }

}
